/**
 * Created by Administrator on 2017/7/20.
 */
import {ty_test} from './checkedDataType.js'
var tyWeekCnArry = ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
var tyDayArry = ['星期天','星期一','星期二','星期三','星期四','星期五','星期六']
window.Date.prototype.getCnDay = function () {
  return tyDayArry[this.getDay()]
}
window.Date.prototype.getCnMonth = function () {
  return tyWeekCnArry[this.getMonth()]
}
function templateData(o) {
  return "<div style='display: inline-block;  color: white;  padding: 0.2rem 0.14rem; font-size: 0.28rem;text-align: center;" +
  " background:rgba(0,0,0,0.71);  border-radius:0.1rem;max-width:40%;min-width: 1.46rem;" +
  " position: absolute;  left: 50%;  top: 50%; box-sizing: border-box; transform:translate(-50%,-50%);  " +
  "-webkit-transform:translate(-50%,-50%);  -o-transform:translate(-50%,-50%);  -moz-transform:translate(-50%,-50%);  " +
  "-ms-transform:translate(-50%,-50%);'>" +
    o +
  "</div>"
}
var globelTempId
function tyTemplate (ty_obj) {
  var newNode = document.createElement('div');
  newNode.id = 'templateWarp';
  var thisValue = (ty_obj.val ? ty_obj.val : '<img src="" ' +
    'style="height: 0.5rem; vertical-align: middle;">');
  newNode.innerHTML = templateData(thisValue)
  newNode.style.cssText = "height:100%;width:100%;top:0;left:0;position: fixed;z-index:9999999999999;";
  var thisBody = document.body;
  if (!document.getElementById('templateWarp')) {
    thisBody.appendChild(newNode);
  } else {
    var ooojj = document.querySelectorAll('#templateWarp>div')[0];
    ooojj.innerHTML = thisValue;
    clearTimeout(globelTempId)
  }
  if (('time' in ty_obj) && (typeof ty_obj.time == 'number')) {
    globelTempId = window.setTimeout(function () {
      var willRemoveNode = document.getElementById('templateWarp');
      thisBody.removeChild(willRemoveNode);
      if (('fn' in ty_obj) && (typeof ty_obj.fn == 'function')) {
        ty_obj.fn()
      }
    }, ty_obj.time)
  }

};
function getMonthI(o, tempTime) {
  var getTempMonth = (o + tempTime.getMonth() + 12) % 12
  var newYear = tempTime.getFullYear()
  if (o > 0) {
    newYear = (getTempMonth === 0) ? (tempTime.getFullYear() + 1) : tempTime.getFullYear()
  } else if (o < 0){
    newYear = (getTempMonth === 11) ? (tempTime.getFullYear() - 1) : tempTime.getFullYear()
  }
  return new Date(newYear, getTempMonth, 1)
}
function getListFn(o) {
  var listArry = []
  var preDays = o.getPreInfo().lastDay.getDate()
  var perFlag = 0
  var nextFlag = o.lastDay.getDay()
  var nowFlag = 0
  while (o.getPreInfo().lastDay.getDay() - perFlag >= 0) {
    listArry.unshift({
      time: new Date(o.getPreInfo().lastDay.getFullYear(), o.getPreInfo().lastDay.getMonth(), preDays - perFlag),
      type: 'per'
    })
    perFlag++
  }
  while (nowFlag < o.lastDay.getDate() + 6 - nextFlag) {
    listArry.push({
      time: new Date(o.lastDay.getFullYear(), o.lastDay.getMonth(), nowFlag + 1),
      type: nowFlag < o.lastDay.getDate()?'now':'next'
    })
    nowFlag++
  }
  return listArry
}
function tyCalendar(ty_data) {
    var tempTime = new Date();
    (ty_test.isUndefined(ty_data) === false) && (tempTime = new Date(ty_test.isNumber(ty_data) ? ty_data : ty_data.replace(/-/g, '/')));
    var tempData = {
        today: tempTime,
        getCalendarList: function () {
          return getListFn(this)
        },
        firstDay: new Date(tempTime.getFullYear(), tempTime.getMonth(), 1),
        lastDay: new Date(getMonthI(1,tempTime).getTime() - 1),
        getPreInfo: function (){
          return tyCalendar(getMonthI(-1,this.today).getTime())
        },
        getNextInfo: function () {
          return tyCalendar(getMonthI(1,this.today).getTime())
        }
    }
    return tempData
}
export default {
  calendar: tyCalendar,
  template: tyTemplate
}
